<?php

require_once WP_PLUGIN_DIR . '/diapucp-events/defines.php' ;
require_once DPEM_SMB_PATH . '/smb_basefield.php' ;

//define( 'DPEM_FIELD_STARTDATE', DPEM . 'startdate' );
//define( 'DPEM_FIELD_ENDDATE', DPEM . 'enddate' );
//define( 'DPEM_FIELD_STARTTIME', DPEM . 'starttime' );
//define( 'DPEM_FIELD_ENDTIME', DPEM . 'endtime' );

define('EVENT_SCHEDULE_CLASS', DPEM . 'event_schedule');

class smartmeta_schedule extends smb_basefieldbox {

    public function  __construct($fields) {
        $this->set_fields( $fields );
        $this->set_inputid( $fields['id'] );

        //these are custom functions on the construct.
        add_action( 'admin_print_styles', array( &$this, 'add_stylesheets' ) );
        add_action( 'admin_enqueue_scripts', array( &$this, 'add_js' ) );
        add_action( 'admin_head-post.php', array( &$this, 'call_js' ) );
        add_action( 'admin_head-post-new.php', array( &$this, 'call_js' ) );
    }

    //Callback for the builder functions.
    public static function build_me( $field_data, &$new_field_object ){
        $new_field_object = new smartmeta_schedule($field_data);
    }

    public function show(){
        $value = $this->fetch_value();
        ?>

        <table class="event_schedule_table" id="<?php echo $this->inputid;?>">
            <tbody>
                <tr>
                    <td>
                        <label for="<?php echo DPEM_FIELD_STARTDATE; ?>"><?php echo $this->fields['name'] ?></label>
                    </td>
                    <td class="<?php echo EVENT_SCHEDULE_CLASS; ?>">
                        <?php $this->add_required_label(); ?>
                        <input
                            id="<?php echo $this->inputid; ?>"
                            name="<?php echo $this->inputid; ?>"
                            value="<?php echo esc_html($value) ?>"
                            type="text"
                            size="8"
                            readonly="yes"
                        />
                    </td>
                    <td class="<?php echo EVENT_SCHEDULE_CLASS; ?>">
                        <label for="<?php echo DPEM_FIELD_STARTTIME ?>"><?php echo $this->fields['name'] ?></label>
                    </td>
                    <td class="<?php echo EVENT_SCHEDULE_CLASS; ?>">
                        <?php $this->add_required_label(); ?>
                        <input
                            id="<?php echo $this->inputid; ?>"
                            name="<?php echo $this->inputid; ?>"
                            value="<?php echo esc_html($value) ?>"
                            type="text"
                            size="8"
                            readonly="yes"
                        />
                    </td>
                </tr>
            </tbody>
        </table>



        <?php
        $this->add_description();
    }

    public function save( $post_id ){
        $data = $_POST[ $this->inputid ];
        if ( empty( $data ) and isset($this->fields['default'] ) ){ $data = $this->fields['default']; }
        $data = wp_strip_all_tags($data);
        smb_basefieldbox::set($this->fields['id'], $data);
    }

    //CSS calls
    public function add_stylesheets(){
        $styleFile = DATEPICK_CSS_FILE_PATH;
        if( file_exists($styleFile) ){
            wp_register_style( 'datepicker-css', DATEPICK_CSS_URL_PATH );
            wp_enqueue_style( 'datepicker-css' );
        }
    }

    //JS calls
    public function add_js(){
        $jsFile = DATEPICK_JSC_FILE_PATH;
        if( file_exists($jsFile) ){
            wp_register_script( 'datepicker-js', DATEPICK_JSC_URL_PATH );
            wp_enqueue_script( 'datepicker-js' );
        }
    }

    public function call_js(){
        if ( !smb_basefieldbox::check_page( 'post' ) ) return;

        echo '<script type="text/javascript">';
            echo 'jQuery(document).ready(function(){';
            echo 'jQuery("#'. $this->inputid .'").datepicker();';
            echo '});';
        echo '</script>';
    }

} //End of the Class

?>